package com.example.nuc_ms_beta.ui.viewconstruction.moderfragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.bumptech.glide.Glide;
import com.example.nuc_ms_beta.R;
import com.example.nuc_ms_beta.app.MyApplication;
import com.example.nuc_ms_beta.bean.newest.Newest_Banner;
import com.example.nuc_ms_beta.bean.newest.Newest_Banner_Ad;
import com.example.nuc_ms_beta.bean.newest.Newest_ListView_New;
import com.example.nuc_ms_beta.bean.newest.Newest_ListView_Top;
import com.example.nuc_ms_beta.ui.activity.UrlActionActivity;
import com.example.nuc_ms_beta.ui.activity.UrlShowActivity;
import com.example.nuc_ms_beta.ui.viewconstruction.adapter.NewestNewListViewAdapter;
import com.example.nuc_ms_beta.ui.viewconstruction.adapter.NewestTopListViewAdapter;
import com.example.nuc_ms_beta.ui.viewconstruction.mainfragment.NewsFragment;

import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.bgabanner.BGABanner;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;


/**
 * Created by 张博川 on 2016/9/27. _____最新资讯______（默认作为主界面第一个）
 * 设计思路：每次自动刷新
 */
public class NewestFragment extends Fragment {

   public NewestFragment(){

   }


    private View view;//主View
    private BGABanner newestfragment_newsbanner, newestfragment_adbanner;
    private ListView newestfragment_top_listView, newestfragment_new_listView;
    /**
     * Data for Top Banner
     */
    private List<String> top_banner_image = new ArrayList<>();
    private List<String> top_banner_title = new ArrayList<>();
    private List<String> top_banner_url = new ArrayList<>();

    /**
     * Data for Bottom Banner
     */
    private List<String> bottom_banner_image = new ArrayList<>();
    private List<String> bottom_banner_title = new ArrayList<>();
    private List<String> getBottom_banner_url = new ArrayList<>();


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        initView(inflater, container);
        return view;
    }

    private void initView(final LayoutInflater inflater, ViewGroup container) {
        view = inflater.inflate(R.layout.newestfragment, container, false);
        //findView for Banner
        newestfragment_newsbanner = (BGABanner) view.findViewById(R.id.newestfragment_newsbanner);
        newestfragment_adbanner = (BGABanner) view.findViewById(R.id.newestfragment_adbanner);
        newestfragment_newsbanner.setAdapter(new BGABanner.Adapter<ImageView, String>() {

            @Override
            public void fillBannerItem(BGABanner banner, ImageView itemView, String model, int position) {
                Glide.with(NewsFragment.context)
                        .load(model)
                        .placeholder(R.mipmap.ic_launcher)
                        .error(R.mipmap.ic_launcher)
                        .centerCrop()
                        .dontAnimate()
                        .into(itemView);
            }
        });
        newestfragment_newsbanner.setDelegate(new BGABanner.Delegate() {
            @Override
            public void onBannerItemClick(BGABanner banner, View itemView, Object model, int position) {
                Intent intent = new Intent(getContext(), UrlShowActivity.class);
                intent.putExtra("url", top_banner_url.get(position));
                startActivity(intent);
            }
        });
        newestfragment_adbanner.setAdapter(new BGABanner.Adapter<ImageView, String>() {

            @Override
            public void fillBannerItem(BGABanner banner, ImageView itemView, String model, int position) {
                Glide.with(NewsFragment.context)
                        .load(model)
                        .placeholder(R.mipmap.ic_launcher)
                        .error(R.mipmap.ic_launcher)
                        .centerCrop()
                        .dontAnimate()
                        .into(itemView);
            }
        });
        newestfragment_adbanner.setDelegate(new BGABanner.Delegate() {
            @Override
            public void onBannerItemClick(BGABanner banner, View itemView, Object model, int position) {
                Intent intent = new Intent(getContext(), UrlShowActivity.class);
                intent.putExtra("url", getBottom_banner_url.get(position));
                startActivity(intent);
            }
        });
        //findView for ListView
        newestfragment_top_listView = (ListView) view.findViewById(R.id.newestfragment_top_listView);
        newestfragment_new_listView = (ListView) view.findViewById(R.id.newestfragment_new_listView);
        queryBannerData();
        queryListViewData();


    }


    public static NewestFragment getInstance() {
        NewestFragment fragment = new NewestFragment();
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    /**
     * Query Data for BGABanner
     */
    public void queryBannerData() {
        //Top BGABAnner
        BmobQuery<Newest_Banner> query_banner_top = new BmobQuery<Newest_Banner>();
        query_banner_top.findObjects(new FindListener<Newest_Banner>() {
            @Override
            public void done(List<Newest_Banner> list, BmobException e) {
                if (e == null) {
                    for (Newest_Banner newest_banner : list) {
                        top_banner_image.add(newest_banner.getBanner_image_file().getFileUrl());
                        top_banner_title.add(newest_banner.getBanner_title());
                        top_banner_url.add(newest_banner.getBanner_url());
                    }
                    newestfragment_newsbanner.setData(top_banner_image, top_banner_title);
                } else {
                    Log.d(MyApplication.TAG, e.getErrorCode() + " " + e.getMessage());
                }
            }
        });
        //Bottom AD BGABAnner
        BmobQuery<Newest_Banner_Ad> query_banner_bottom = new BmobQuery<>();
        query_banner_bottom.findObjects(new FindListener<Newest_Banner_Ad>() {
            @Override
            public void done(List<Newest_Banner_Ad> list, BmobException e) {
                if (e == null) {
                    for (Newest_Banner_Ad newest_banner_ad : list) {
                        bottom_banner_image.add(newest_banner_ad.getBanner_file_image().getFileUrl());
                        bottom_banner_title.add(newest_banner_ad.getBanner_title());
                        getBottom_banner_url.add(newest_banner_ad.getBanner_url());
                    }
                    newestfragment_adbanner.setData(bottom_banner_image, bottom_banner_title);
                } else {
                    Log.d(MyApplication.TAG, e.getErrorCode() + " " + e.getMessage());
                }
            }
        });
    }

    /**
     * Query Data for ListView
     */
    public void queryListViewData() {
        //NEWS
        BmobQuery<Newest_ListView_New> query_listView_new = new BmobQuery<>();
        query_listView_new.findObjects(new FindListener<Newest_ListView_New>() {

            @Override
            public void done(final List<Newest_ListView_New> list, BmobException e) {
                if (e == null) {
                    newestfragment_new_listView.setAdapter(new NewestNewListViewAdapter(getContext(), list, R.layout.newestfragment_listview_item));
                    newestfragment_new_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent intent = new Intent(MyApplication.context, UrlActionActivity.class);
                            intent.putExtra("url", list.get(position).getListview_contenturl());
                            intent.putExtra("objectId", list.get(position).getObjectId());
                            startActivity(intent);
                        }
                    });
                } else {
                    Log.d(MyApplication.TAG, e.getErrorCode() + " " + e.getMessage());
                }
            }
        });

        //TOPS

        BmobQuery<Newest_ListView_Top> query_listView_top = new BmobQuery<>();
        query_listView_top.findObjects(new FindListener<Newest_ListView_Top>() {
            @Override
            public void done(final List<Newest_ListView_Top> list, BmobException e) {
                if (e == null) {
                    newestfragment_top_listView.setAdapter(new NewestTopListViewAdapter(getContext(), list, R.layout.newestfragment_listview_item));
                    newestfragment_top_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent intent = new Intent(MyApplication.context, UrlActionActivity.class);
                            intent.putExtra("url", list.get(position).getListview_contenturl());
                            intent.putExtra("objectId", list.get(position).getObjectId());
                            startActivity(intent);
                        }
                    });
                } else {
                    Log.d(MyApplication.TAG, e.getErrorCode() + " " + e.getMessage());
                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        top_banner_title.clear();
        top_banner_image.clear();
        bottom_banner_title.clear();
        bottom_banner_image.clear();
    }
}
